<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;

use View;
use App\Model\Area;


class AreaController extends AdminBaseController
{
    public function __construct()
    {
        $option = ['module_name'=>'area','status'=>'area_status'];
        $this->setModelObj(new Area,$option);
    }

    /**
     * 后台区域列表
     */
    public function listData(Request $request,$id=0)
    {
        $parent_areainfo = $id ? Area::where('area_id',$id)->first() : '';
        return View::make('admin.area.arealist', ['areainfo'=>$parent_areainfo]);
    }

    /**
     * 后台区域列表（Bootstrap-table 异步获取数据）
     */
    public function bootstrapTableList(Request $request, $id=0)
    {
        $data = $request->all();
        $skip = $data['pagenum'];
        $take = $data['limitnum'];
        $orderby = $this->getTrueOrder($data);
        $arealist = (object)[];
        $arealist->data = Area::where('area_parent_id',$id)->orderBy('area_status','desc')->orderBy($orderby['field'],$orderby['type'])->skip($skip)->take($take)->get();
        $arealist->total = Area::where('area_parent_id',$id)->count();
        $area_list_json = json_encode($arealist);
        return $area_list_json;
    }

    /**
     * 获取正确的排序组合
     */
    protected function getTrueOrder($data)
    {
        $orderby = [];
        if(!array_key_exists('sort',$data)){
            $orderby['field'] = 'area_sort';
            $orderby['type'] = 'asc';
        }else{
            switch ($data['sort']){
                case 'sort_format':
                    $orderby['field'] = 'area_sort';break;
                default:
                    $orderby['field'] = $data['sort'];break;
            }
            $orderby['type'] =  $data['order'];
        }
        return $orderby;
    }

    /**
     * 后台区域添加页面
     */
    public function addDataView($data='0_1')
    {
        $arr = explode('_',$data);
        $arr['parentid'] = $arr[0];
        $arr['type'] = $arr[1];
        return View::make('admin.area.areaadd',['info'=>$arr]);
    }

    /**
     * 后台区域添加保存
     */
    public function toAddData(Requests\AreaRequest $request)
    {
        $data = $request->except(['_token','_method']);
        $data['area_status'] = 1;
        $re = Area::insert($data);
        $jsonData = $re ? $this->getJsonData(2,'增加成功') : $this->getJsonData(0,'增加失败');
        return response()->json($jsonData);
    }

    /**
     * 后台区域编辑保存
     */
    public function toUpdateData(Requests\AreaRequest $request, $id)
    {
        $data = $request->except(['_token','_method']);
        $re = Area::where('area_id',$id)->update($data);
        $jsonData = $re ? $this->getJsonData(2,'编辑成功') : $this->getJsonData(0,'编辑失败');
        return response()->json($jsonData);
    }

    /**
     * 后台区域排序保存
     */
    public function toSortData(Request $request)
    {
        $data = $request->except(['_token','_method']);
        if(!is_numeric($data['content'])){
            $jsonData = $this->getJsonData(0,'输入的数据不合法');
        }else{
            $re = Area::where('area_id',$data['id'])->update(['area_sort'=>(int)$data['content']]);
            $jsonData = $re ? $this->getJsonData(3,'编辑成功') : $this->getJsonData(0,'编辑失败');
        }
        return response()->json($jsonData);
    }

    /**
     * 判断数据是否可删除
     */
    protected function enableToDel($ids)
    {
        $exit_child = Area::whereIn('area_parent_id',$ids)->first();
        return ($exit_child ? false : true);
    }
}